// shopMallPackage/pages/addressList/addressList.js
import { queryReceiveAddressList, deleteReceiveAddress, } from "../../../service/onetoone_service"
import {
  queryAddressList,
  setDefaultAddress,
  deleteAddress
} from "../../../service/jb_address_service"
import Toast from '../../../dist/toast/toast';
import Dialog from '../../../dist/dialog/dialog';
import {
  areaList
} from "../../commonData/areaList"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [

    ],
    isSelect: false, // 是否从选择页面来的

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        isSelect: true
      })
    }
    if (!wx.getStorageSync("areaList")) {
      wx.setStorage({
        key: "areaList",
        data: areaList,
        success: function () {
          console.log('存储城市列表成功');
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryReceiveAddressList();
  },
  //查询地址列表:
  queryReceiveAddressList() {
    wx.showLoading();
    queryAddressList().then(res => {
      console.log("JJLog:收货地址列表===:" + JSON.stringify(res));
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          dataList: res.data.list || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryAddressList", err);
    })
  },
  setDefaultAddressAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.dataList[index];
    //如果数量只有一个就必须是默认。
    if (this.data.dataList.length == 1 && item.isDefault) {
      return;
    }

    if (this.data.dataList[index].isDefault) {
      return;
    }
    item.isDefault = !item.isDefault

    wx.showLoading();
    setDefaultAddress({ id: item.id }).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        // wx.showToast({
        //   title: '设置成功',
        //   icon:"none"
        // })
        Toast.success('设置成功');
        let arr = this.data.dataList;
        arr.forEach(i => {
          i.isDefault = false;
        })
        arr[index].isDefault = true;
        this.setData({
          dataList: arr,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "设置默认地址失败",
        icon: "none"
      })
      console.error("JJLog:setDefaultAddress", err);
    })
  },
  //删除一个地址
  deleteAddressAction(e) {
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    Dialog.confirm({
      title: '提示',
      message: '确认删除该地址？',
    })
      .then(() => {
        wx.showLoading();
        deleteAddress({ id: id }).then(res => {
          wx.hideLoading();
          if (res.code == 200) {
            Toast.success('删除成功');
            let arr = this.data.dataList;
            arr.splice(index, 1);
            this.setData({
              dataList: arr
            })
          }
        }).catch(err => {
          wx.hideLoading();
          wx.showToast({
            title: "删除地址失败",
            icon: "none"
          })
          console.error("JJLog:deleteAddress", err);
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  // 跳转到编辑页面:
  pushToEdit(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/shopMallPackage/pages/addAddress/addAddress?id=' + id,
    })
  },
  selectAddressAction(e) {
    if (!this.data.isSelect) {
      return;
    }
    let item = e.currentTarget.dataset.item;
    app.globalData.selectAddressData = item;
    wx.navigateBack();
  },
  // 返回选择的上一个页面
  backToSelect(e) {
    if (this.data.isSelect) {
      let id = e.currentTarget.dataset.id;
      app.globalData.selectAddressId = id;
      wx.navigateBack();
    }

  },
  cancelAddressAction(e) {
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认删除当前地址吗?',
      success: res => {
        if (res.confirm) {
          this.deleteReceiveAddress(id);
        } else if (res.cancel) {

        }
      }
    })
  },
  deleteReceiveAddress(id) {
    wx.showLoading();
    deleteReceiveAddress(id).then(res => {
      console.log("JJLog:删除地址===:" + JSON.stringify(res));
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '删除成功',
          icon: "success"
        })
        this.queryReceiveAddressList();
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "删除失败",
        icon: "none"
      })
      console.error("JJLog:deleteReceiveAddress", err);
    })
  },

  // 跳转到添加地址
  pushToAddAddress() {
    wx.navigateTo({
      url: '/shopMallPackage/pages/addAddress/addAddress',
    })
  },
})